package com.lyl.myaiagent.servertools.tools;

import cn.hutool.core.io.FileUtil;
import com.lyl.myaiagent.servertools.tools.constants.FileConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * @author lyl
 * @version 1.0
 * @CreateTime: 2025-08-19
 */
@Slf4j
public class FileOperationTool {

    private static final String File_DIR = FileConstants.FILE_SAVE_DIR + "/file";

    @Tool(description = "read content of file")
    public String readFile(@ToolParam(description = "name of the file") String fileName){
        log.info("开始读取文件");
        String filePath = File_DIR + "/" + fileName;
        try{
            return FileUtil.readUtf8String(filePath);
        }catch (Exception e){
            log.info("读取文件有误");
            return "Error reading content of the file" + e.getMessage();
        }
    }


    @Tool(description = "write content to file")
    public String writeFile(@ToolParam(description = "name of the file") String fileName,
                            @ToolParam(description = "content of the file") String fileContent){
        log.info("开始写入文件");
        String filePath = File_DIR + "/" + fileName;
        // 创建目录
        try {
            FileUtil.mkdir(File_DIR);
            FileUtil.writeUtf8String(fileContent, filePath);
            return "File successfully write to:" + filePath;
        } catch (Exception e) {
            log.error("写入文件有误");
            return "Error write file to:" + fileContent + "  " + e.getMessage();
        }
    }

}
